package com.st.mall.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.st.mall.user.model.UserModel;
import com.st.mall.user.service.IUserService;
import com.st.mall.util.MD5;
import com.st.mall.util.controller.AuthCodeController;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private IUserService service;
	
	/**
	 * 管理员添加用户
	 * @param userModel
	 * @return
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public String insert(UserModel userModel) {
		userModel.setPass(getDefaultPass());	//默认密码+加密
		String result = service.insert(userModel);
		System.out.println(result);
		return result;
	}
	
	private String getDefaultPass() {
		return MD5.encode("123456");
	}
	
	/**
	 * 重置密码
	 */
	@RequestMapping("/resetPas")
	@ResponseBody
	public int resetPas() {
		String pas = getDefaultPass();
		if(MD5.encode("123456").equals(pas)) {
			return 1;
		}else return 0;
	}
	/**
	 * 用户注册
	 * @param userModel
	 * @param authcode
	 * @param req
	 * @return
	 */
	@RequestMapping("/reg")
	@ResponseBody
	public String reg(UserModel userModel,String authcode,HttpServletRequest req) {
		Object authCodeSession = req.getSession().getAttribute(AuthCodeController.name);
		userModel.setPass(MD5.encode(userModel.getPass()));	//加密
		String result = service.reg(userModel,authcode,authCodeSession);
		System.out.println(result);
		return result;
	}
	
	/**
	 * 登录
	 * @param userModel
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public String login(UserModel userModel,String authcode,HttpServletRequest req,HttpSession session) {
		Object authCodeSession = req.getSession().getAttribute(AuthCodeController.name);
		userModel.setPass(MD5.encode(userModel.getPass()));// 加密
		String result = service.login(userModel,authcode,authCodeSession);
		if(result.equals("C")||result.equals("S")) {
			UserModel user = service.selectId(userModel);
			session.setAttribute("user", user);
		}
		return result;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public int delete(String code) {
		int result = service.delete(code);
		return result;
	}
	
	/**
	 * 分页查询
	 * @param userModel
	 * @return
	 */
	@RequestMapping(value = "/selectModel",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String selectModel(UserModel userModel) {
		List<UserModel> list = service.selectModel(userModel);
		int result = service.selectCount(userModel);
		Map<String,Object> map = new HashMap<>();
		map.put("data", list);
		map.put("count", result);
		return new JSONObject(map).toString();
	}
	
	/**
	 * 更新
	 * @param userModel
	 * @return
	 */
	@RequestMapping(value = "/update",produces = "application/json;charset=utf-8")
	@ResponseBody
	public int update(UserModel userModel) {
		int result = service.update(userModel);
		return result;
	}
	
	/**
	 * 回显
	 * @param userModel
	 * @return
	 */
	@RequestMapping(value = "/get",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String get(UserModel userModel) {
		UserModel model = service.selectId(userModel);
		model.setPass("");
		return new JSONObject(model).toString();
	}
	
	/**注销
	 * @param session
	 * @return
	 */
	@RequestMapping("/userOut")
	public String loginOut(HttpSession session) {
		session.removeAttribute("user");
		return "front/login";
	}
	
}
